<template>
  <div class="result-container">
    <div class="title-wrap">
      <h2 class="title">{{this.$route.query.q}}</h2>
      <span class="sub-title">
        找到{{total}}个结果
      </span>
    </div>
    <el-tabs v-model="activeIndex"
    loading @tab-click="handleClick">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table"  loading>
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tr class="el-table__row"
          v-for="(songObj, index) in songsList" :key="index"
          @dblclick="playMusic(songObj.id)">
            <td>{{index+1}}</td>
            <td>
              <div class="song-wrap">
                <div class="name-wrap">
                  <span>{{songObj.name}}</span>
                  <!-- mv的图标 -->
                  <span
                  v-if="songObj.mvid!=0" class="iconfont icon-mv"></span>
                </div>
                <span
                v-if="songObj.alias.length !=0">{{songObj.alias[0]}}</span>
              </div>
            </td>
            <td>{{songObj.artists[0].name}}</td>
            <td>{{songObj.album.name}}</td>
            <td>{{songObj.duration | changeTime}}</td>
          </tr>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div class="item"
          v-for="(playListObj, index) in playList"
          :key="index"
          @click="toDetail(playListObj.id)">
            <div class="img-wrap">
              <div class="num-wrap">
                <span class="num">{{playListObj.playCount | changeCount}}</span>
              </div>
              <img :src="playListObj.coverImgUrl" alt=""/>
              <span class="iconfont icon-play" @click="playMusic(playListObj.id)"></span>
            </div>
            <p class="name">{{playListObj.name}}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div class="item"
          v-for="(mvObj,index) in mvList"
          :key="index"
          @click="toMvDetail(mvObj.id)">
            <div class="img-wrap">
              <img :src="mvObj.cover" alt=""/>
              <span
              @click="playMusic(mvObj.id)" class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div
                  @click="playMusic(mvObj.id)"
                  class="iconfont icon-play"></div>
                <div class="num">{{mvObj.playCount | changeCount}}</div>
              </div>
              <span class="time">{{mvObj.duration | changeTime}}</span>
            </div>
            <div class="info-wrap">
              <div class="name">{{mvObj.name}}</div>
              <div class="singer">{{mvObj.artistName}}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data () {
    return {
      activeIndex: 'songs',
      songsList: [],
      // 歌单
      playList: [],
      // mv
      mvList: [],
      // 搜索结果的总数
      total: 0
    }
  },
  filters: {
    changeTime (value) {
      let min = parseInt(value / 1000 / 60)
      if (min < 10) min = '0' + min
      let sec = parseInt(value / 1000 % 60)
      if (sec < 10) sec = '0' + sec
      const time = `${min}分${sec}秒`
      return time
    },
    changeCount (value) {
      if (value > 100000) {
        const count = parseInt(value / 100000) + '万'
        return count
      }
    }
  },
  watch: {
    activeIndex () {
      // songs
      // lists 歌单
      // mv
      let type = 1
      // 获取个数
      let limit = 10
      switch (this.activeIndex) {
        case 'songs':
          type = 1
          limit = 10
          break
        case 'lists':
          type = 1000
          limit = 10
          break
        case 'mv':
          type = 1004
          limit = 8
          break
        default:
          break
      }
      this.loadSearchMv(type, limit)
    }
  },
  created () {
    this.loadSearchMv()
  },
  methods: {
    loadSearchMv (type, limit) {
      axios({
        url: 'https://autumnfish.cn/search',
        method: 'get',
        params: {
          keywords: this.$route.query.q,
          type,
          // 获取的数据量
          limit
        }
      }).then(res => {
        // console.log(res)
        if (type === 1) {
          const { songs, songCount } = res.data.result
          this.songsList = songs
          this.total = songCount
        } else if (type === 1000) {
          const { playlists, playlistCount } = res.data.result
          this.playList = playlists
          this.total = playlistCount
        } else if (type === 1004) {
          const { mvs, mvCount } = res.data.result
          this.mvList = mvs
          this.total = mvCount
        }
      })
    },
    // 切换栏
    handleClick () {},
    // 播放音乐
    playMusic (id) {
      axios({
        url: 'https://autumnfish.cn/song/url',
        method: 'get',
        params: {
          id
        }
      }).then(res => {
        this.$parent.musicUrl = res.data.data[0].url
      })
    },
    toDetail (id) {
      // 页面跳转
      this.$router.push(`/details?q=${id}`)
    },
    toMvDetail (id) {
      this.$router.push(`/mvDetail?q=${id}`)
    }
  }
}
</script>
